<?php
namespace App\Http\Controllers\AdminApi;

use App\Model\AdminTable;
use App\Model\ClassTable;
use App\Model\CouponListTable;
use App\Model\CouponTable;
use App\Model\UserLevelSetTable;
use App\Model\UserTable;
use App\Repository\BaseController\ApiController;
use App\Repository\Common\ConstTable;
use App\Repository\Sys\JsonRespose;
use App\Repository\Sys\Str;

class UserLevelController extends ApiController
{


    /**
     * @api {get} /adminapi/userlevel/list 等级列表
     * @apiGroup Admin-Level
     * @apiDescription  说明: 无
     *
     * @apiParam {Int} page --
     * @apiParam {Int} page_num --
     *
     * @apiSuccessExample {json} Success-Response:
     *
     * {
    "code": 1,
    "msg": "操作成功!",
    "data": {
    "count": "3",
    "list": [
    {
    "id": "3",
    "start_integral": "444",
    "end_integral": "444",
    "level_name": "金",
    "discount": "7.00"
    },
    {
    "id": "2",
    "start_integral": "222",
    "end_integral": "333",
    "level_name": "银",
    "discount": "8.00"
    },
    {
    "id": "1",
    "start_integral": "11",
    "end_integral": "111",
    "level_name": "铜",
    "discount": "9.00"
    }
    ],
    "page": 1
    }
    }
     */
    public function getList()
    {
        $page = \Request::get('page', 1);
        $limit = \Request::get('page_num', 10);
        $param = [];
        $where = '1=1';
        $count = \DB::select("select count(*) as c  from user_level_set WHERE {$where}", $param)[0]->c;
        $param['offset'] = ($page - 1) * $limit;
        $param['limit'] = $limit;
        $res = \DB::select("select * from user_level_set WHERE {$where} ORDER BY id DESC limit :offset,:limit", $param);

        $list = [];
        foreach ($res as $v) {

            $list[] = $v;
        }
        $data = [
            'count' => $count,
            'list' => $list,
            'page' => $page,
        ];
        return JsonRespose::success($data);
    }

    /**
     * @api {post} /adminapi/userlevel/save 添加修改
     * @apiGroup Admin-Level
     * @apiDescription  说明: 无
     *
     * @apiParam {Int} id 可选,需要修改的id
     * @apiParam {String} start_integral 开始积分
     * @apiParam {String} end_integral 结束积分
     * @apiParam {String} level_name 等级名称
     * @apiParam {String} discount 折扣
     *
     * @apiSuccessExample {json} Success-Response:
     *
     *  {
     * "code": 1,
     * "msg": "操作成功!",
     * "data": 3
     * }
     */
    public function postSave()
    {
        $id = \Request::get('id');
        $start_integral = \Request::get('start_integral');
        $end_integral = \Request::get('end_integral');
        $level_name = \Request::get('level_name');
        $discount = \Request::get('discount');


        if ($id) {
            $res = UserLevelSetTable::ORM()->where('id', $id)->update([
                'start_integral' => $start_integral,
                'end_integral' => $end_integral,
                'level_name' => $level_name,
                'discount' => $discount,
            ]);
        } else {
            $res = UserLevelSetTable::ORM()->insertGetId([
                'start_integral' => $start_integral,
                'end_integral' => $end_integral,
                'level_name' => $level_name,
                'discount' => $discount,
            ]);
        }
        return JsonRespose::success($res);
    }

    /**
     * @api {post} /adminapi/userlevel/del 删除等级配置
     * @apiGroup Admin-Level
     * @apiDescription  说明: 无
     *
     * @apiParam {Int} id  --
     *
     * @apiSuccessExample {json} Success-Response:
     *
     *  {
     * "code": 1,
     * "msg": "操作成功!",
     * "data": []
     * }
     */
    public function postDel()
    {
        $id = \Request::get('id');
        $res = UserLevelSetTable::ORM()->where('id', $id)->delete();
        if ($res) {
            return JsonRespose::success();
        } else {
            return JsonRespose::fail();
        }

    }


}